/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jy.springbootcmsservice.modules.sys.entity;

import com.jy.springbootcmsservice.common.base.entity.DataEntity;
import com.jy.springbootcmsservice.common.config.Global;
import com.jy.springbootcmsservice.common.constant.Punctuation;
import com.jy.springbootcmsservice.common.utils.CollectionsUtils;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 角色Entity
 *
 * @author tm
 * @version 2020-11-23
 */
@Data
public class Role extends DataEntity<Role> {

    /**
     * 数据范围（1：所有数据；2：所在公司及以下数据；3：所在公司数据；4：所在部门及以下数据；5：所在部门数据；8：仅本人数据；9：按明细设置）
     */
    public static final String DATA_SCOPE_ALL = "1";
    public static final String DATA_SCOPE_COMPANY_AND_CHILD = "2";
    public static final String DATA_SCOPE_COMPANY = "3";
    public static final String DATA_SCOPE_OFFICE_AND_CHILD = "4";
    public static final String DATA_SCOPE_OFFICE = "5";
    public static final String DATA_SCOPE_SELF = "8";
    public static final String DATA_SCOPE_CUSTOM = "9";

    /**
     * 归属机构
     */
    private Office office;
    /**
     * 角色名称
     */
    private String name;
    /**
     * 英文名称
     */
    private String enName;
    /**
     * 权限类型
     */
    private String roleType;
    /**
     * 数据范围
     */
    private String dataScope;

    /**
     * 原角色名称
     */
    private String oldName;
    /**
     * 原英文名称
     */
    private String oldEnName;
    /**
     * 是否是系统数据
     */
    private String sysData;
    /**
     * 是否是可用
     */
    private String useAble;

    /**
     * 根据用户ID查询角色列表
     */
    private User user;

    /**
     * 拥有菜单列表
     */
    private List<Menu> menuList = new ArrayList<>();
    /**
     * 按明细设置数据范围
     */
    private List<Office> officeList = new ArrayList<>();

    public Role() {
        super();
        this.dataScope = DATA_SCOPE_SELF;
        this.useAble = Global.YES;
    }

    public Role(String id) {
        super(id);
    }

    public Role(User user) {
        this();
        this.user = user;
    }

    public List<String> getMenuIdList() {
        List<String> menuIdList = new ArrayList<>();
        for (Menu menu : menuList) {
            menuIdList.add(menu.getId());
        }
        return menuIdList;
    }

    public void setMenuIdList(List<String> menuIdList) {
        menuList = new ArrayList<>();
        for (String menuId : menuIdList) {
            Menu menu = new Menu();
            menu.setId(menuId);
            menuList.add(menu);
        }
    }

    public String getMenuIds() {
        return StringUtils.join(getMenuIdList(), Punctuation.COMMA);
    }

    public void setMenuIds(String menuIds) {
        menuList = new ArrayList<>();
        if (menuIds != null) {
            String[] ids = StringUtils.split(menuIds, Punctuation.COMMA);
            setMenuIdList(CollectionsUtils.createNewList(ids));
        }
    }

    public List<Office> getOfficeList() {
        return officeList;
    }

    public void setOfficeList(List<Office> officeList) {
        this.officeList = officeList;
    }

    public List<String> getOfficeIdList() {
        List<String> officeIdList = new ArrayList<>();
        for (Office office : officeList) {
            officeIdList.add(office.getId());
        }
        return officeIdList;
    }

    public void setOfficeIdList(List<String> officeIdList) {
        officeList = new ArrayList<>();
        for (String officeId : officeIdList) {
            Office office = new Office();
            office.setId(officeId);
            officeList.add(office);
        }
    }

    public String getOfficeIds() {
        return StringUtils.join(getOfficeIdList(), Punctuation.COMMA);
    }

    public void setOfficeIds(String officeIds) {
        officeList = new ArrayList<>();
        if (officeIds != null) {
            String[] ids = StringUtils.split(officeIds, Punctuation.COMMA);
            setOfficeIdList(CollectionsUtils.createNewList(ids));
        }
    }

    /**
     * 获取权限字符串列表
     */
    public List<String> getPermissions() {
        List<String> permissions = new ArrayList<>();
        for (Menu menu : menuList) {
            if (menu.getPermission() != null && !"".equals(menu.getPermission())) {
                permissions.add(menu.getPermission());
            }
        }
        return permissions;
    }

}
